package com.shelpe.services.sinbad.entity.jpa;

import java.util.Date;

import javax.persistence.AttributeConverter;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.shelpe.services.sinbad.controller.LogDto.LogNoteable;

import lombok.Data;

@Entity(name = "message_topics")
@Data
public class MessageTopics implements LogNoteable{
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	private long messageID;
	@Column(name = "content_type")
	private String type;
	private String title;
	private String content;
	@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createTime;
	@Convert(converter = FlagFormConverter.class)
	private FlagForm flagForm;// 消息的标签形式
	@Convert(converter = DialogStatusConverter.class)
	private DialogStatus isDialog;// 是否弹窗;1:弹窗 0:不弹窗(默认)

	public static enum DialogStatus {
		notDialog(0), dialog(1);

		private int value;

		private DialogStatus(int v) {
			this.value = v;
		}

		public int getValue() {
			return this.value;
		}

		public static DialogStatus from(String value) {
			DialogStatus dialogStatus = DialogStatus.notDialog;
			for (DialogStatus s : DialogStatus.values()) {
				if (s.name().equals(value)) {
					dialogStatus = s;
					break;
				}
			}
			return dialogStatus;
		}

		public static DialogStatus to(int value) {
			DialogStatus dialogStatus = DialogStatus.notDialog;
			for (DialogStatus s : DialogStatus.values()) {
				if (s.getValue() == value) {
					dialogStatus = s;
					break;
				}
			}
			return dialogStatus;
		}
	}

	public static enum FlagForm {
		envelope("envelope"), bulb("bulb");
		private String value;

		private FlagForm(String v) {
			this.value = v;
		}

		public String getValue() {
			return this.value;
		}

		public static FlagForm from(String value) {
			FlagForm en = FlagForm.envelope;
			for (FlagForm s : FlagForm.values()) {
				if (s.getValue().equals(value)) {
					en = s;
					break;
				}
			}
			return en;
		}
	}

	public static class DialogStatusConverter implements AttributeConverter<DialogStatus, Integer> {

		@Override
		public Integer convertToDatabaseColumn(DialogStatus attribute) {
			return attribute != null ? attribute.getValue() : DialogStatus.notDialog.getValue();
		}

		@Override
		public DialogStatus convertToEntityAttribute(Integer dbData) {
			return DialogStatus.to(dbData);
		}
	}

	public static class FlagFormConverter implements AttributeConverter<FlagForm, String> {

		@Override
		public String convertToDatabaseColumn(FlagForm attribute) {
			return attribute != null ? attribute.getValue() : FlagForm.envelope.name();
		}

		@Override
		public FlagForm convertToEntityAttribute(String dbData) {
			if (StringUtils.isNotBlank(dbData)) {
				return FlagForm.from(dbData);
			}
			return null;
		}
	}
}
